package com.yalantis.euclid.sample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.leon.lfilepickerlibrary.LFilePicker;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;


import java.io.File;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class HomeActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Button button_send = (Button) findViewById(R.id.button_send);
        button_send.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                new LFilePicker()
                        .withActivity(HomeActivity.this)
                        .withRequestCode(1)
                        .withTitle("选择文件")//标题文字
                        .withMutilyMode(true)
                        .withFileFilter(new String[]{".jpg",".png","jpeg"})
                        .start();
            }
        });
        Button button_result = (Button) findViewById(R.id.button_result);
        button_result.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(HomeActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                List<String> list = data.getStringArrayListExtra("paths");
//                Toast.makeText(getApplicationContext(), "选中了" + list.size() + "个文件", Toast.LENGTH_SHORT).show();
                try
                {
                    postFile(list);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    public void postFile(List<String> list) throws Exception{

//        String path ="sdcard/Download/cache_files/kabuka_201.apk";
//        String path2 ="sdcard/DCIM/mm.jpg";
//        File file = new File(path);
//        File file2 = new File(path2);
//        if(file.exists() && file.length()>0){
//            System.out.println("文件找到！");

        //创建异步请求对象
        AsyncHttpClient client = new AsyncHttpClient();
        //将要上传的文件放入RequestParams对象中
        RequestParams params = new RequestParams();
        File[] files= new File[list.size()];
        for (int i = 0;i < list.size();i++)
        {
            files[i] = new File(list.get(i));
        }
        params.put("file", files);
//        params.put("file", file);
//        params.put("file", file2);
//        params.put("content", "wujie");
        //发起post请求
        client.post("http://172.20.52.233:8080/KnowHer/picUpdate.json", params,new AsyncHttpResponseHandler() {
            //上传失败监听方法
            int count = 0;
            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                                  Throwable arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(HomeActivity.this, "分析失败", Toast.LENGTH_LONG).show();
            }
            //上传成功监听方法
            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                // TODO Auto-generated method stub
                ContentString.resultJson = new String(arg2);
                Toast.makeText(HomeActivity.this, "分析完成", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(HomeActivity.this,MainActivity.class);
                startActivity(intent);
            }
            //上传过程中的进度
            public void onProgress(int bytesWritten, int totalSize) {
                // TODO Auto-generated method stub
                super.onProgress(bytesWritten, totalSize);
                count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
                Log.i("上传 Progress>>>>>", count + " / ");
            }

            @Override
            public void onRetry(int retryNo) {
                // TODO Auto-generated method stub
                super.onRetry(retryNo);
                // 返回重试次数
            }
        });
//        }else{
//            Toast.makeText(HomeActivity.this, "文件不存在", Toast.LENGTH_LONG).show();
//        }
    }
}
